using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Xml;

using PHSRAG.Utility;
using DataAccess = PHSRAG.Utility.DataAccess;
using IRBProtocolList = IRBInfo.IRBProtocolList;
using BasicIRBInfo = IRBInfo.BasicIRBInfo;

[WebService(Namespace = "http://phsrag.partners.org")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {
    }

    [WebMethod(Description = "Returns Status, ApprovalDate, ExpirationDate, Protocol Desctription, Institution, Sponsor Name" +
                "Principal Investigator and a list of people associated with protocol, role, permission and email")]
    public string GetBasicInfoForIRBNumber(string folderNumber)
    {
        try
        {
            BasicIRBInfo basicIRBInfo = new BasicIRBInfo();

            DataAccess.LoadTypedDataSet(basicIRBInfo, DataAccess.ConnectionString, "GetBasicInfoIRB",
                DataAccess.BuildSqlParameterArray("@folderNumber", SqlDbType.VarChar, folderNumber));

            if (basicIRBInfo.BasicIRBProtocolInfo.Rows.Count > 0)
            {
                return basicIRBInfo.GetXml();
            }
            else
            {
                StringWriter writer = new StringWriter();
                XmlTextWriter xwriter = new XmlTextWriter(writer);
                xwriter.WriteStartDocument(false);
                xwriter.WriteStartElement("Message");
                xwriter.WriteString("Protocol not found");
                xwriter.WriteEndElement();
                xwriter.WriteEndDocument();

                return writer.ToString();
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Trace.WriteLine(ExceptionReport.GetExceptionInfo(e, false));
            throw;
        }
    }

    [WebMethod(Description = "Returns a list of IRB Protocol numbers associated with a person with studyDescription, sponsorName and status",
            MessageName = "ListIRBNumbersForPerson")]
    public string ListIRBNumbersForPerson(string userName)
    {
        try
        {
            IRBProtocolList protocolList = new IRBProtocolList();

            DataAccess.LoadTypedDataSet(protocolList, DataAccess.ConnectionString, "GetProtocolsListForPerson",
                DataAccess.BuildSqlParameterArray("@userName", SqlDbType.VarChar, userName));


            if (protocolList.ProtocolList.Rows.Count > 0)
            {
                return protocolList.GetXml();
            }
            else
            {
                StringWriter writer = new StringWriter();
                XmlTextWriter xwriter = new XmlTextWriter(writer);
                xwriter.WriteStartDocument(false);
                xwriter.WriteStartElement("Message");
                xwriter.WriteString("No protocols associated with this person");
                xwriter.WriteEndElement();
                xwriter.WriteEndDocument();

                return writer.ToString();
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Trace.WriteLine(ExceptionReport.GetExceptionInfo(e, false));
            throw;
        }
    }

    [WebMethod(Description = "Returns list of IRB Protocol numbers associated with a person " +
            "for the specified protocol Status with studyDescription, sponsorName and status", MessageName = "ListIRBNumbersForPersonAndStatus")]
    public string ListIRBNumbersForPersonAndStatus(string userName, string status)
    {
        try
        {
            IRBProtocolList protocolList = new IRBProtocolList();
            DataAccess.LoadTypedDataSet(protocolList, DataAccess.ConnectionString, "GetProtocolsListForPerson",
                DataAccess.BuildSqlParameterArray("@userName", SqlDbType.VarChar, userName,
                "@status", SqlDbType.VarChar, status));

            if (protocolList.ProtocolList.Rows.Count > 0)
            {
                return protocolList.GetXml();
            }
            else
            {
                StringWriter writer = new StringWriter();
                XmlTextWriter xwriter = new XmlTextWriter(writer);
                xwriter.WriteStartDocument(false);
                xwriter.WriteStartElement("Message");
                xwriter.WriteString("No protocols associated with this person");
                xwriter.WriteEndElement();
                xwriter.WriteEndDocument();

                return writer.ToString();
            }
        }
        catch (Exception e)
        {
            System.Diagnostics.Trace.WriteLine(ExceptionReport.GetExceptionInfo(e, false));
            throw;
        }
    }    
}
